/*
 * @Author: xuhongling
 * @Date:   2020-11-19 15:42:54
 * @Last Modified by:   xuhongling
 * @Last Modified time: 2022-06-13 15:12:13
 */

//水位转蓄水量    z:当前水位，tList:水位蓄量关系曲线数组
const zToW = (z, tList) => {
  let result = '';
  if (tList.length <= 0) {
    return;
  }
  if (!z) {
    return;
  }
  for (let i = 0; i < tList.length; i++) {
    if (z <= tList[i].rz) {
      if (z === tList[i].rz) {
        result = tList[i].w;
      } else {
        let d1 = tList[i - 1];
        let d2 = tList[i];
        let x0 = parseFloat(d1.rz);
        let y0 = parseFloat(d1.w);
        let x1 = parseFloat(d2.rz);
        let y1 = parseFloat(d2.w);
        result = y0 + ((x0 - z) / (x0 - x1)) * (y1 - y0);
      }
      break;
    }
  }
  return result.toFixed(3);
};

//蓄水量转水位     w:当前蓄水量，tList:水位蓄量关系曲线数组
const wToZ = (w, tList) => {
  let result = '';
  if (tList.length <= 0) {
    return;
  }
  if (!w) {
    return;
  }
  if (w <= tList[0].w) {
    result = tList[0].rz;
  } else if (w >= tList[tList.length - 1].w) {
    result = tList[tList.length - 1].rz;
  } else {
    for (let i = 0; i < tList.length; i++) {
      if (w <= tList[i].w) {
        if (w == tList[i].w) {
          result = tList[i].rz;
        } else {
          let d1 = tList[i - 1];
          let d2 = tList[i];
          let x0 = parseFloat(d1.w);
          let y0 = parseFloat(d1.rz);
          let x1 = parseFloat(d2.w);
          let y1 = parseFloat(d2.rz);
          result = y0 + ((x0 - w) / (x0 - x1)) * (y1 - y0);
        }
        break;
      }
    }
  }
  return result.toFixed(2);
};

//水位转流量     z当前水位，tList水位流量曲线
const zToQ = (z, tList) => {
  let result = '';
  if (tList.length <= 0) {
    return '';
  }
  if (!z) {
    return '';
  }
  if (z > tList[tList.length - 1]['z'] || z < tList[0]['z']) {
    return '';
  }
  for (let i = 0; i < tList.length; i++) {
    if (z <= tList[i]['z']) {
      if (z == tList[i]['z']) {
        result = tList[i]['q'];
      } else {
        let d1 = tList[i - 1];
        let d2 = tList[i];
        let x0 = parseFloat(d1['z']);
        let y0 = parseFloat(d1['q']);
        let x1 = parseFloat(d2['z']);
        let y1 = parseFloat(d2['q']);
        result = y0 + ((x0 - z) / (x0 - x1)) * (y1 - y0);
      }
      break;
    }
  }
  return result.toFixed(3);
};

//流量转水位      当前流量，tList水位流量曲线
const qToZ = (q, tList) => {
  let result = '';
  if (tList.length <= 0) {
    return '';
  }
  if (!q) {
    return '';
  }
  if (q <= tList[0]['q']) {
    result = tList[0]['z'];
  } else if (q >= tList[tList.length - 1]['q']) {
    result = tList[tList.length - 1]['z'];
  } else {
    for (var i = 0; i < tList.length; i++) {
      if (q <= tList[i]['q']) {
        if (q == tList[i]['q']) {
          result = tList[i]['z'];
        } else {
          var d1 = tList[i - 1];
          var d2 = tList[i];
          var x0 = parseFloat(d1['q']);
          var y0 = parseFloat(d1['z']);
          var x1 = parseFloat(d2['q']);
          var y1 = parseFloat(d2['z']);
          result = y0 + ((x0 - q) / (x0 - x1)) * (y1 - y0);
        }
        break;
      }
    }
  }
  return result.toFixed(2);
};

export { zToW, wToZ, zToQ, qToZ };
